Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    tabbarActive: 0,
    selected: 0,
    tabList: [
      {
        selectedIconPath: "/tabbarIcon/indexSelect.png",
        iconPath: "/tabbarIcon/index.png",
        pagePath: "/pages/home/home"
      },
      {
        selectedIconPath: "/tabbarIcon/mapSelect.png",
        iconPath: "/tabbarIcon/map.png",
        pagePath: "/pages/map/map"
      },
      {
        iconPath: "/tabbarIcon/add.png",
        selectedIconPath: "/tabbarIcon/add.png",
        pagePath: "/pages/add/add"
      },
      {
        selectedIconPath: "/tabbarIcon/emergencySelect.png",
        iconPath: "/tabbarIcon/emergency.png",
        pagePath: "/pages/emergency/emergency"
      },
      {
        selectedIconPath: "/tabbarIcon/mineSelect.png",
        iconPath: "/tabbarIcon/mine.png",
        pagePath: "/pages/mine/mine"
      }
    ]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    switchTab(e) { 
      const data = e.currentTarget.dataset
      let url = data.path
      //用于表示是否能跳转
      let jump = true;
      //用于表示是否跳转了
      let jumped = true;
      //在用户将要跳转到非首页的页面时，检验用户是否登录
      if(data.index != 0){
        if(!wx.getStorageSync('cookieKey')){
          jump = false;
          wx.showModal({
            cancelColor: 'cancelColor',
            title: '您尚未登录',
            content: '是否跳转到登录界面？',
            cancelText: '否',
            confirmText: '是',
            success(res) {
              if (res.confirm) {
                console.log('用户点击确定')
                wx.navigateTo({
                  url: '../login/login',
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }
      }
      if(data.index==2 && wx.getStorageSync('cookieKey')){ 
        jump = false;
        wx.showActionSheet({
            itemList: ['校园咨询', '失物招领', '寻物启事','物品供需','求跑腿'],
            success (res) {
              switch(res.tapIndex)
              {
                case 0: 
                wx.navigateTo({
                  url: '../addConsult/addConsult'
                });
                break;
                case 1:
                wx.navigateTo({
                  url: '../addLost/addLost'
                });
                break;
                case 2:
                wx.navigateTo({
                  url: '../addFind/addFind'
                });
                case 3:
                  wx.navigateTo({
                    url: '../addObject/addObject'
                  });
                case 4:
                  wx.navigateTo({
                    url: '../addRun/addRun'
                  });
                break;
                case 3:
                  wx.navigateTo({
                    url: '../addObject/addObject'
                  });
                  break;
                case 4:
                  wx.navigateTo({
                    url: '../addRun/addRun'
                  });
                break;
              }
              // if(res.tapIndex==0){
              //   wx.navigateTo({
              //     url: '../addConsult/addConsult'
              //   })
              // }
              // console.log(res.tapIndex)
            }, 
            fail (res) {
              console.log(res.errMsg)
            } 
          })
          // url = "../" 
      }

      //页面跳转到本页
      if(jump){
        wx.switchTab({url})
      }
      this.setData({
        if (jumped) {
          selected: data.index
        }
      })
    }
  }
})
